package com.jspx.security.sm;


/**
 SM4分组密码算法是我国自主设计的分组对称密码算法，用于实现数据的加密/解密运算，以保证数据和信息的机密性。
 要保证一个对称密码算法的安全性的基本条件是其具备足够的密钥长度，SM4算法与AES算法具有相同的密钥长度分组长度128比特，因此在安全性上高于3DES算法。
 */

import com.jspx.boot.environment.Environment;
import com.jspx.security.utils.EncryptUtil;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class SM4Utils
{
	private String secretKey = "";

	private String iv = "";

	private boolean hexString = false;

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	public void setIv(String iv) {
		this.iv = iv;
	}

	public void setHexString(boolean hexString) {
		this.hexString = hexString;
	}

	public SM4Utils()
	{
	}

	public String encryptData_ECB(String plainText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.ENCRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = EncryptUtil.hexToByte(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes(Environment.defaultEncode);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(Environment.defaultEncode));
			String cipherText = EncryptUtil.getBase64Encode(encrypted,EncryptUtil.DEFAULT);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public String decryptData_ECB(String cipherText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.DECRYPT;

			byte[] keyBytes;
			if (hexString)
			{
				keyBytes = EncryptUtil.hexToByte(secretKey);
			}
			else
			{
				keyBytes = secretKey.getBytes(Environment.defaultEncode);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, EncryptUtil.getBase64Decode(cipherText));
			return new String(decrypted, Environment.defaultEncode);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public String encryptData_CBC(String plainText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.ENCRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = EncryptUtil.hexToByte(secretKey);
				ivBytes = EncryptUtil.hexToByte(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes(Environment.defaultEncode);
				ivBytes = iv.getBytes(Environment.defaultEncode);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes(Environment.defaultEncode));
			String cipherText = EncryptUtil.getBase64Encode(encrypted,EncryptUtil.DEFAULT);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Pattern p = Pattern.compile("\\s*|\t|\r|\n");
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public String decryptData_CBC(String cipherText)
	{
		try
		{
			SM4_Context ctx = new SM4_Context();
			ctx.isPadding = true;
			ctx.mode = SM4.DECRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = EncryptUtil.hexToByte(secretKey);
				ivBytes = EncryptUtil.hexToByte(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes(Environment.defaultEncode);
				ivBytes = iv.getBytes(Environment.defaultEncode);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, EncryptUtil.getBase64Decode(cipherText));
			return new String(decrypted, Environment.defaultEncode);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws IOException
	{
		String plainText = "abcd中文要加密的文档";

		SM4Utils sm4 = new SM4Utils();

		sm4.secretKey = "JeF8U9wHFOMfs2Y8";
		sm4.hexString = false;

		System.out.println("ECB模式");
		String cipherText = sm4.encryptData_ECB(plainText);
		System.out.println("密文: " + cipherText);
		System.out.println("");

		plainText = sm4.decryptData_ECB(cipherText);
		System.out.println("明文: " + plainText);
		System.out.println("");

		System.out.println("CBC模式");
		sm4.iv = "UISwD9fW6cFh9SNS";
		cipherText = sm4.encryptData_CBC(plainText);
		System.out.println("密文: " + cipherText);
		System.out.println("");

		plainText = sm4.decryptData_CBC(cipherText);
		System.out.println("明文: " + plainText);
	}
}
